<?php
	//variables
	require_once('includes.php');
	include_once('check_oauth_function.php');
	include_once('date_conversion_function.php');
	session_start();
	
	//switch function to do some selection from calendar.php
	switch($_GET['action']) {
		case 'add':
			addEvent();
			break;
		case 'update':
			updateEvent();
			break;
		case 'resize':
			resizeEvent();
			break;
		case 'del':
			removeEvent();
			break;
		case 'get':
			getEvent();
			break;	
	}

	//This function used to get calendar ID from google server
	function getCalendarID() {
		$client = new apiClient();
		$client->setUseObjects(true);

		$service = new apiCalendarService($client);
		
		if (isset($_SESSION['token'])) {
			$client->setAccessToken($_SESSION['token']);
		}
		
		if ($client->getAccessToken()) {
			$calList = $service->calendarList->listCalendarList();
		
			while(true) {
				foreach ($calList->getItems() as $calendarListEntry) {
					$title = $calendarListEntry->getSummary();
					if($title === 'dangertracker') {
						$calId = $calendarListEntry->getId();
					}
				}
				$pageToken = $calList->getNextPageToken();
				if ($pageToken) {
					$optParams = array('pageToken' => $pageToken);
					$calList = $calList->calendarList->listCalendarList($optParams);
				} else {
					break;
				}
			}
			$_SESSION['token'] = $client->getAccessToken();
		}
		return $calId;
	}

	//To retreive all the event on user calendar if no event return null
	function getEvent() {
		$client = new apiClient();
		$client->setApplicationName('Google Calendar for Health Tracker');
		$client->setUseObjects(true);
		$service = new apiCalendarService($client);
		
		if (isset($_SESSION['token'])) {
			$client->setAccessToken($_SESSION['token']);
		}
		
		if ($client->getAccessToken()) {
			$calId = getCalendarID();
			$eventList = $service->events->listEvents($calId);
			if($eventList->getItems()) {
				while(true) {
					foreach ($eventList->getItems() as $event) {
						$arr_line = array(
							'id' => $event->getId(),
							'title' => $event->getSummary(),
							'start_date' => convertDate($event->getStart()->getDateTime()),
							'time_start' => convertTime($event->getStart()->getDateTime()),
							'end_date' => convertDate($event->getEnd()->getDateTime()),
							'time_end' => convertTime($event->getEnd()->getDateTime()),
							'allDay' => false,
							'start' => convertDateTime($event->getStart()->getDateTime()),
							'end' => convertDateTime($event->getEnd()->getDateTime()),
							'desc' => $event->getDescription(),
							'editable' => true);
						$arr_content[] = $arr_line;
					}
					$pageToken = $eventList->getNextPageToken();
					if ($pageToken) {
						$optParams = array('pageToken' => $pageToken);
							$eventList = $service->events->listEvents(calId, $optParams);
					} else {
						break;
					}
				}
				echo json_encode($arr_content);
			} else {
				echo json_encode(null);
			}
			$_SESSION['token'] = $client->getAccessToken();
		}
	}
	
	//function used to add event to google calendar
	function addEvent() {
	
		$client = new apiClient();
		$client->setApplicationName('Google Calendar for Health Tracker');
		$client->setUseObjects(true);

		$service = new apiCalendarService($client);

		if (isset($_SESSION['token'])) {
			$client->setAccessToken($_SESSION['token']);
		}

		if ($client->getAccessToken()) {
			$calID = getCalendarID();
			
			//convert date from form to a google datetime
			$datestart = convertDateTime($_POST['date_start']);
			$dateend = convertDateTime($_POST['date_end']);
			
			//create a blank event
			$event = new Event();
			//set title of event
			$event->setSummary($_POST['title']);
			//set description of the event / note
			$event->setDescription($_POST['note']);
			$start = new EventDateTIme();
			$start->setDateTime($datestart);
			$event->setStart($start);
			$end = new EventDateTIme();
			$end->setDateTime($dateend);
			$event->setEnd($end);
			
			//insert to google calendar
			$createdEvent = $service->events->insert($calID, $event);
			
			$_SESSION['token'] = $client->getAccessToken();
		}
	}
	
	//function to remove an event from calendar
	function removeEvent() {
		$client = new apiClient();
		$client->setUseObjects(true);

		$service = new apiCalendarService($client);
		
		if (isset($_SESSION['token'])) {
			$client->setAccessToken($_SESSION['token']);
		}
		
		if ($client->getAccessToken()) {
			$calID = getCalendarID();
			
			$service->events->delete($calID, $_POST['event_id']);
			
			$_SESSION['token'] = $client->getAccessToken();
		}
	}
	
	//function to update an entry on certain event
	function updateEvent() {
		$client = new apiClient();
		$client->setApplicationName('Google Calendar for Health Tracker');
		$client->setUseObjects(true);

		$service = new apiCalendarService($client);
		
		if (isset($_SESSION['token'])) {
			$client->setAccessToken($_SESSION['token']);
		}
		
		if ($client->getAccessToken()) {
		
			$datestart = convertDateTime($_POST['date_start']);
			$dateend = convertDateTime($_POST['date_end']);
			//event id on calendar event
			$id = $_POST['event_id'];
			//calendar id
			$calID = getCalendarID();
			
			//get the particular event provided event_id and calendar id
			$event = $service->events->get($calID, $id);
			
			$event->setSummary($_POST['title']);
			$event->setDescription($_POST['note']);
			$start = new EventDateTIme();
			$start->setDateTime($datestart);
			$event->setStart($start);
			$end = new EventDateTIme();
			$end->setDateTime($dateend);
			$event->setEnd($end);

			//update the eevet
			$service->events->update($calID, $event->getId(), $event);
			
			$_SESSION['token'] = $client->getAccessToken();
		}
	}
?>	
	